﻿using d1Common.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace d1Common
{
    public class doServices
    {
        private static doServices singleInstance = new doServices();
        public static doServices Instance
        {
            get
            {
                return singleInstance;
            }
        }

        List<IDoModleService> allModules = new List<IDoModleService>();
        public void InitServices()
        {
            Type _typeIDoModleService = typeof(IDoModleService);
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    foreach (var t in type.GetInterfaces())
                    {
                        if (t == _typeIDoModleService)
                        {
                            IDoModleService _obj = Activator.CreateInstance(type) as IDoModleService;
                            this.allModules.Add(_obj);
                            break;
                        }
                    }
                }
            }
            foreach (IDoModleService _moduleService in allModules)
            {
                _moduleService.onServiceInit();
            }
            foreach (IDoModleService _moduleService in allModules)
            {
                _moduleService.onServiceLoaded();
            }
        }

        public IDoCore DoCore
        {
            get;
            private set;
        }

        public IDoStorage DoStorage
        {
            get;
            private set;
        }

        public IDoConfig DoConfig
        {
            get;
            private set;
        }

        public IDoLog DoLog
        {
            get;
            private set;
        }

        public IDo4AService Do4AService
        {
            get;
            private set;
        }

        public IDoCache DoCache
        {
            get;
            private set;
        }


        private Dictionary<Type, Type> Types = new Dictionary<Type, Type>();
        private Dictionary<Type, object> Instances = new Dictionary<Type, object>();

        public void RegisterInstance<TInterface>(TInterface _instance)
        {
            Instances[typeof(TInterface)] = _instance;
            if (_instance is IDoCore)
            {
                DoCore = _instance as IDoCore;
                return;
            }
            if (_instance is IDoConfig)
            {
                DoConfig = _instance as IDoConfig;
                return;
            }
            if (_instance is IDoLog)
            {
                DoLog = _instance as IDoLog;
                return;
            }
            if (_instance is IDo4AService)
            {
                Do4AService = _instance as IDo4AService;
                return;
            }
            if (_instance is IDoStorage)
            {
                DoStorage = _instance as IDoStorage;
                return;
            }
            if (_instance is IDoCache)
            {
                DoCache = _instance as IDoCache;
                return;
            }
        }

        public List<IDoModleService> GetAllModuleServices()
        {
            List<IDoModleService> _results = new List<IDoModleService>();
            _results.AddRange(this.allModules);
            return _results;
        }
    }
}